package com.token.iot.bratterycharging.boss.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.outstanding.framework.base.TransactionLogger;
import com.outstanding.framework.container.springmvc.WebContext;
import com.outstanding.framework.core.PendingException;
import com.token.base.device.api.OnlineState;
import com.token.iot.bratterycharging.boss.service.DeviceService;
import com.token.iot.bratterycharging.gateway.api.Device;

@Controller
@RequestMapping("/device")
public class DeviceController {

	
	@Autowired
	private DeviceService deviceService;
	
	
	@ResponseBody
	@RequestMapping(value = "/add.do")
	public DeviceDTO insertDevice(@RequestBody DeviceDTO dto) throws PendingException {
		TransactionLogger.debug("staring insertDevice " + String.valueOf(dto == null) + " ");
		Long operid = (Long) WebContext.getSession().getAttribute("_operid");
		if (operid == null) {
			throw new PendingException(ResCode.OperNotLogin);

		}
		return deviceService.addDevice(dto, operid);
	}
	
	
	/**
	 * 将设备分发
	 * 
	 * @param dto
	 * @return
	 * @throws PendingException
	 */
	@ResponseBody
	@RequestMapping(value = "/dis.do")
	public DeviceDTO distributionDevice(@RequestBody DeviceDTO dto) throws PendingException {
		TransactionLogger.debug("staring distributionDevice " + String.valueOf(dto == null) + " ");
		String agentid = (String) WebContext.getSession().getAttribute("_agentid");
		if (agentid == null) {
			throw new PendingException(ResCode.OperNotLogin);
		}
		dto.setAgentId(agentid);
		return deviceService.distributionDevice(dto);
	}

	/**
	 * 设备签收
	 * 
	 * @param dto
	 * @return
	 * @throws PendingException
	 */
	@ResponseBody
	@RequestMapping(value = "/checkin.do")
	public DeviceDTO checkinDevice(@RequestBody DeviceDTO dto) throws PendingException {
		TransactionLogger.debug("staring checkin " + String.valueOf(dto == null) + " ");
		String agentid = (String) WebContext.getSession().getAttribute("_agentid");
		if (agentid == null) {
			throw new PendingException(ResCode.OperNotLogin);
		}
		dto.setAgentId(agentid);
		return deviceService.checkinDevice(dto);
	}
	
	
	/**
	 * 设备列表
	 * 
	 * @param dto
	 * @return
	 * @throws PendingException
	 */
	@ResponseBody
	@RequestMapping(value = "/list.do")
	public PageInfo list(@RequestBody ReqPageDto dto) throws PendingException {
		TransactionLogger.debug("staring checkin " + String.valueOf(dto == null) + " ");
		Long operid = (Long) WebContext.getSession().getAttribute("_operid");
		if (operid == null) {
			String agentid = (String) WebContext.getSession().getAttribute("_agentid");
			if (agentid == null) {
				String mid = (String) WebContext.getSession().getAttribute("_merchantid");
				if(mid == null) {
					throw new PendingException(ResCode.OperNotLogin);
				}else {
					return deviceService.listByMerchantId(dto.getPageNo(), dto.getPageSize(), mid);
				}
			}else {
				return deviceService.listByAgentId(dto.getPageNo(), dto.getPageSize(), agentid);
			}
		}else {
			return deviceService.listall(dto.getPageNo(), dto.getPageSize());
		}
		
	}
	
	
	/**
	 * 设备列表
	 * 
	 * @param dto
	 * @return
	 * @throws PendingException
	 */
	@ResponseBody
	@RequestMapping(value = "/query.do")
	public PageInfo query(@RequestBody ReqPageDto dto) throws PendingException {
		TransactionLogger.debug("staring query " + String.valueOf(dto == null) + " ");
		Long operid = (Long) WebContext.getSession().getAttribute("_operid");
		if (operid == null) {
			String agentid = (String) WebContext.getSession().getAttribute("_agentid");
			if (agentid == null) {
				String mid = (String) WebContext.getSession().getAttribute("_merchantid");
				if(mid == null) {
					throw new PendingException(ResCode.OperNotLogin);
				}
			}
		}
		return deviceService.query(dto.getPageNo(), dto.getPageSize(),dto);
	
	}
	
	/**
	 * 设备列表
	 * 
	 * @param dto
	 * @return
	 * @throws PendingException
	 */
	@ResponseBody
	@RequestMapping(value = "/detail.do")
	public DeviceDTO detail(String imei) throws PendingException {
		TransactionLogger.debug("staring detail " + imei + " ");
		Long operid = (Long) WebContext.getSession().getAttribute("_operid");
		if (operid == null) {
			String agentid = (String) WebContext.getSession().getAttribute("_agentid");
			if (agentid == null) {
				String mid = (String) WebContext.getSession().getAttribute("_merchantid");
				if(mid == null) {
					throw new PendingException(ResCode.OperNotLogin);
				}
			}
		}
		return deviceService.selectOne(imei);
	}
}
